package games.mapacman.common;

import games.mapacman.client.GameScreen;
import games.mapacman.server.MaPacmanRPWorld;
import games.mapacman.server.MaPacmanZone;
import marauroa.common.game.Definition;
import marauroa.common.game.RPClass;
import marauroa.common.game.RPObject;
import marauroa.common.game.Definition.Type;
import marauroa.common.game.IRPZone;
import marauroa.server.game.Statistics;

public class Dot {

    public static final int TYPE_DOT = 1;
    public static final int TYPE_SUPERDOT = 2;
    public static final int TYPE_FRUIT = 3;
    public static final int TYPE_POWERPILL = 4;
    protected int x, y;
    protected int dot_score;
    protected MaPacmanZone zone;
    protected RPObject myObject;

    public Dot(int x, int y, int dot_score, MaPacmanZone zone) {
        myObject = new RPObject();
        myObject.put("type", Constants.TYPE_DOT);
        myObject.put("x", x);
        myObject.put("y", y);
        myObject.put("score", dot_score);
        myObject.put("respawn", -1);
        myObject.put("zoneid", zone.getName());
        this.x = x;
        this.y = y;
        this.dot_score = dot_score;
        this.zone = zone;
    }

    public Dot(RPObject object) {
        myObject = object;
        for (IRPZone zone : MaPacmanRPWorld.get()) {
            MaPacmanZone maPacmanZone = (MaPacmanZone)zone;
            if (maPacmanZone.getName().compareTo(object.get("zoneid")) == 0) {
                this.zone = maPacmanZone;
                break;
            }
        }
        x = object.getInt("x");
        y = object.getInt("y");
    }

    public static void initRPClass() {
        RPClass dot = new RPClass(Constants.TYPE_DOT);
        dot.isA("position");
        dot.addAttribute("score", Type.BYTE, Definition.VOLATILE);
        dot.addAttribute("respawn", Type.INT);
    }

    public RPObject getRPObject() {
        return myObject;
    }

    public int getX() {
        return myObject.getInt("x");
    }

    public int getY() {
        return myObject.getInt("y");
    }

    public void setRespawnTime() {
        setRespawnTime(zone.getDotRespawnTime());
    }

    public void setRespawnTime(int dot_respawnTime) {
        myObject.put("respawn", dot_respawnTime);

    }

    public boolean respawn() {
        myObject.put("respawn", myObject.getInt("respawn") - 1);

        return (myObject.getInt("respawn") < 1);
    }

    public int getScore() {
        return myObject.getInt("score");
    }

    public void addStats(Statistics stats) {
        stats.add("Dots eaten", 1);
    }

    public void draw(GameScreen screen) {
        screen.draw(screen.getDotSprite(TYPE_DOT), x, y);
    }

    public void eaten(RPObject object) {
        // nothing happens to player when Dot is eaten
    }
}
